﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Base.Common;

namespace Base.Data
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>Refer http://rookian.com/wordpressBlog/index.php/2010/09/25/paging-with-nhibernate-3-0-using-the-new-query-api-queryover/</remarks>
    public static class QueryOverExtensions
    {
        public static PagedList<T> ToPagedList<T>(this IQueryOver<T, T> queryOver, int pageIndex, int pageSize)
        {
            var rowCountQuery = queryOver.ToRowCountQuery();
            //IEnumerable<T> list = queryOver.Take(pageSize).Skip((pageIndex - 1) * pageSize).Future();
            IEnumerable<T> list = queryOver.Take(pageSize).Skip((pageIndex - 1) * pageSize).List();           
            int totalCount = rowCountQuery.FutureValue<int>().Value;
            return new PagedList<T>(list as List<T>, pageSize, pageIndex, totalCount);
        }
    }    
}
